package controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import service.examinfo.ExaminfoService;
import service.interest.InterestService;
import service.majorunit.MajorunitService;
import service.member.MemberService;
import service.notice.NoticeService;
import service.position.PositionService;
import service.review.ReviewService;
import util.returnvo.ExaminfoListReturnVO;
import util.returnvo.InterestListReturnVO;
import util.returnvo.NoticeListReturnVO;
import util.returnvo.ReviewListReturnVO;

public class MemberContoller extends MultiActionController{
	
	private MemberService memberService;
	private InterestService interestService;
	private MajorunitService majorunitService;
	private NoticeService noticeService;
	private ReviewService reviewService;
	private ExaminfoService examinfoService;
	private PositionService positionService;
	
	public void setExaminfoService(ExaminfoService examinfoService) {
		this.examinfoService = examinfoService;
	}

	public void setReviewService(ReviewService reviewService) {
		this.reviewService = reviewService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public void setMajorunitService(MajorunitService majorunitService) {
		this.majorunitService = majorunitService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	
	public void setInterestService(InterestService interestService) {
		this.interestService = interestService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}

	/**
	 *login(String member_id, String member_pass)를 호출해 넘어온 member_no가 0보다 클 경우
	 *세션에 user이라는 이름으로 MemberVO를 바인딩하고 메시지 리턴.
	 *0보다 작을 경우 맞춰서 메시지 리턴 
	 */
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String mail = request.getParameter("member_mail");
		String password = request.getParameter("member_pass");
		String message = null;
		int member_no = memberService.login(mail, password);
		if(member_no>0){
			request.getSession().setAttribute("member_no", Integer.toString(member_no));
			message = "로그인에 성공했습니다";
		}else if(member_no<0){
			if(member_no==-1){
				message = "패스워드가 틀렸습니다";
			}else{
				message = "존재하지 않는 회원입니다";
			}
		}
		return new ModelAndView("JsonView", "message", message);
	}
	
	/**
	 * 알림페이지로 이동
	 */
	public ModelAndView notice(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String page=request.getParameter("page");
		NoticeListReturnVO noticeList=noticeService.getNoticeList(member_no, page);
		return new ModelAndView("member/notice", "noticeList", noticeList);
	}
	
	/**
	 * 관심학과페이지로 이동
	 */
	public ModelAndView interest(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String page=request.getParameter("page");
		InterestListReturnVO interestList=majorunitService.getInterestedUnit(member_no, page);
		return new ModelAndView("member/interest", "interestList", interestList);
	}
	
	/**
	 * 내 리뷰 페이지로 이동
	 */
	public ModelAndView myReview(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String page=request.getParameter("page");
		ReviewListReturnVO reviewList=reviewService.getMyReviewList(member_no, page);
		
		ModelAndView mv=new ModelAndView("member/my_review", "reviewList", reviewList);
		
		mv.addObject("positionList", positionService.getAllPosition());
		return mv;
	}
	
	/**
	 * 내 입시 페이지로 이동
	 */
	public ModelAndView myExam(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String page=request.getParameter("page");
		ExaminfoListReturnVO examList=examinfoService.getMyExaminfos(member_no, page);
		return new ModelAndView("member/my_exam", "examList", examList);
	}
	
	/**
	 * 로그아웃
	 */
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) throws Exception{
		request.getSession().invalidate();
		return null;
	}
	
	/**
	 * ajax로 즐겨찾기 여부 검색. 뷰에서 javascript로 받아서 즐겨찾기 여부를 표시하는 함수를 만들어 놓고 페이지 로딩시와 즐겨찾기 클릭시에 호출 시키자.
	 */
	public ModelAndView isInterested(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		return new ModelAndView("JsonView", "result", interestService.isInterested(member_no, majorunit_no));
	}
	
	/**
	 * ajax로 즐겨찾기 등록/해제후 결과값
	 */
	public ModelAndView interestUnit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		int majorunit_no=Integer.parseInt(request.getParameter("majorunit_no"));
		boolean result=interestService.interestUnit(member_no, majorunit_no);
		return new ModelAndView("JsonView", "result", result);
	}
	
	/**
	 * 즐겨찾기 해제
	 */
	public ModelAndView uninterestUnit(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String[] majorunit_no=request.getParameterValues("majorunit_no");
		interestService.uninterestUnit(member_no, majorunit_no);
		return null;
	}
	
	/**
	 * 회원가입. 1이면 이미 존재하는 메일입니다 보냄. 0이면 가입되었습니다 인증해주세요. 추천인을 받은 경우 추천인 readable 해제
	 */
	public ModelAndView registerMember(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String mail = request.getParameter("member_mail2");
		String password = request.getParameter("member_pass2");
		String message = null;
		int result = memberService.registerMember(mail, password);
		if(result==-1){
			message = "이미 존재하는 회원입니다.";
		}else {
			message = "회원가입이 완료되었습니다";
			request.getSession().setAttribute("member_no", Integer.toString(result));
		}
		return new ModelAndView("JsonView", "message2", message);
	}
	
	/**
	 * 비밀번호 찾기. 결과에 따라 메세지 실어보냄.
	 */
	public ModelAndView findPass(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		return null;
	}
	
	/**
	 * 정보수정페이지로.
	 */
	public ModelAndView updateMember(HttpServletRequest request, HttpServletResponse response) throws Exception{
		return new ModelAndView("member/update_member");
	}


	/**
	 * 비밀번호 변경. member_id를 받아오기 위해 보안 명목으로 아이디를 한번 더 입력 시키자.
	 */
	public ModelAndView updatePass(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int member_no=Integer.parseInt((String)request.getSession().getAttribute("member_no"));
		String member_pass=request.getParameter("member_pass");
		return new ModelAndView("JsonView", "result", memberService.updatePass(member_no, member_pass));
	}

	/**
	 * 알림 읽음
	 */
	public ModelAndView readNotice(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int notice_no=Integer.parseInt(request.getParameter("notice_no"));
		noticeService.readNotice(notice_no);
		return null;
	}
	
	/**
	 * 알림 삭제
	 */
	public ModelAndView deleteNotice(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String[] noticeArr=request.getParameterValues("notice_no");
		noticeService.deleteNotice(noticeArr);
		return null;
	}
}
